package com.bawei.a2010douyin;

import android.os.Bundle;
import android.util.Log;
import android.view.MenuItem;
import android.widget.Toast;

import com.alibaba.android.arouter.launcher.ARouter;
import com.baidu.location.BDAbstractLocationListener;
import com.baidu.location.BDLocation;
import com.baidu.location.LocationClient;
import com.baidu.location.LocationClientOption;
import com.bawei.a2010douyin.adapter.MyFragmentPagerAdapter;
import com.bawei.a2010douyin.annotation.LoginCheck;
import com.bawei.a2010douyin.base.http.ApiServer;
import com.bawei.a2010douyin.base.http.RetrofitManager;
import com.bawei.a2010douyin.base.mvp.BaseActivity;
import com.bawei.a2010douyin.base.mvp.BaseMvpActivity;
import com.bawei.a2010douyin.base.mvp.BasePresenter;
import com.bawei.a2010douyin.base.rx.BaseFunc;
import com.bawei.a2010douyin.base.utils.ARouterPath;
import com.bawei.a2010douyin.entity.BaseResponse;
import com.bawei.a2010douyin.entity.VideoEntity;
import com.bawei.a2010douyin.ui.fragment.FriendsFragment;
import com.bawei.a2010douyin.ui.fragment.HomeFragment;
import com.bawei.a2010douyin.ui.fragment.MeFragment;
import com.bawei.a2010douyin.ui.fragment.SheQuFragment;
import com.blankj.utilcode.util.SPUtils;
import com.google.android.material.bottomnavigation.BottomNavigationView;
import com.google.android.material.navigation.NavigationBarView;

import java.util.ArrayList;
import java.util.List;

import androidx.annotation.NonNull;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentStatePagerAdapter;
import androidx.viewpager.widget.ViewPager;
import io.reactivex.Observable;
import io.reactivex.ObservableSource;
import io.reactivex.Observer;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.functions.Consumer;
import io.reactivex.functions.Function;
import io.reactivex.schedulers.Schedulers;

/**
 框架：
 1。所有依赖和权限：
 2。国际化设置
 3。屏幕适配
 4。冷启动优化
 5。沉浸式状态栏(mvp封装)：BaseActivity中，
 6。网络封装
 7。BaseResponse封装
 8。glide封装
 9。mvp封装
 10。Arouter配置
 11。GreenDao ORM框架
 12。EventBus事件总线封装



 */
public class MainActivity extends BaseActivity{
    private ViewPager viewPager;
    private BottomNavigationView bottom;
    private List<Fragment> mList = new ArrayList<>();
    private MyFragmentPagerAdapter mMyFragmentPagerAdapter;
    public LocationClient mLocationClient = null;
    private BDAbstractLocationListener myListener = new BDAbstractLocationListener() {
        @Override
        public void onReceiveLocation(BDLocation location) {
            String addr = location.getAddrStr();    //获取详细地址信息
            //定位成功将位置存储到SP中
            SPUtils.getInstance().put("location",addr);
        }
    };


    @Override
    public int bindLayout() {
        return R.layout.activity_main;
    }

    @Override
    public void initView(Bundle savedInstanceState) {
        initLocation();




        viewPager = (ViewPager) findViewById(R.id.view_pager);
        bottom = (BottomNavigationView) findViewById(R.id.bottom);
        mList.add(new HomeFragment());
        mList.add(new FriendsFragment());
        mList.add(new SheQuFragment());
        mList.add(new MeFragment());
        mMyFragmentPagerAdapter = new MyFragmentPagerAdapter(getSupportFragmentManager(), FragmentStatePagerAdapter.BEHAVIOR_RESUME_ONLY_CURRENT_FRAGMENT,mList);
        viewPager.setAdapter(mMyFragmentPagerAdapter);
        //上下联动
        viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                if(position == 0){
                    bottom.getMenu().getItem(0).setChecked(true);
                }else if(position == 1){
                    bottom.getMenu().getItem(1).setChecked(true);
                }else if(position == 2){
                    bottom.getMenu().getItem(3).setChecked(true);
                }else if(position == 3){
                    bottom.getMenu().getItem(4).setChecked(true);
                }
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
        //点击底部
        bottom.setOnItemSelectedListener(new NavigationBarView.OnItemSelectedListener() {
            @Override
            public boolean onNavigationItemSelected(@NonNull MenuItem item) {
                switch (item.getItemId()){
                    case R.id.home:
                        viewPager.setCurrentItem(0);
                        break;
                    case R.id.friends:
                        viewPager.setCurrentItem(1);
                        break;
                    case R.id.start:
                        startPush();
                        break;
                    case R.id.shequ:
                        viewPager.setCurrentItem(2);
                        break;
                    case R.id.me:
                        me();
                        break;
                }
                return true;

            }
        });
        //默认红点
        bottom.getOrCreateBadge(R.id.home).setNumber(45);
    }
    //定位:建议定位开启服务
    private void initLocation() {
        mLocationClient = new LocationClient(getApplicationContext());
        //声明LocationClient类
        mLocationClient.registerLocationListener(myListener);
        //注册监听函数
        LocationClientOption option = new LocationClientOption();

        option.setIsNeedAddress(true);
//可选，是否需要地址信息，默认为不需要，即参数为false
//如果开发者需要获得当前点的地址信息，此处必须为true

        option.setNeedNewVersionRgc(true);
//可选，设置是否需要最新版本的地址信息。默认需要，即参数为true

        mLocationClient.setLocOption(option);
//mLocationClient为第二步初始化过的LocationClient对象
//需将配置好的LocationClientOption对象，通过setLocOption方法传递给LocationClient对象使用
//更多LocationClientOption的配置，请参照类参考中LocationClientOption类的详细说明
        mLocationClient.start();
    }

    @LoginCheck
    private void me() {
        viewPager.setCurrentItem(3);
    }
    @LoginCheck
    private void startPush() {
        ARouter.getInstance().build(ARouterPath.PUSH_AROUTER_PATH).navigation();

    }

    @Override
    public void initData() {

    }
}